﻿using System.Drawing;
using System.Windows.Forms;
using System;

namespace Library.Winform.Draw.Tools {
  public abstract class ToolDraw : ToolGraphic {
    protected ObjectDraw Current;
    protected abstract ObjectDraw CreateObjectOnMouseDown(IDrawContainer container, ScrollMouseEventArgs e);
    protected abstract UInt16 PointHandlerOnMouseMove(ScrollMouseEventArgs e);
    protected override void OnMouseDown(IDrawContainer container, ScrollMouseEventArgs e) {
      Current = CreateObjectOnMouseDown(container, e);
      AddNewObject(container, Current);
    }


    /// <summary>
    /// Add new object to draw area.
    /// Function is called when user left-clicks draw area,
    /// and one of ToolObject-derived tools is active.
    /// </summary>
    /// <param name="container"></param>
    /// <param name="o"></param>

    protected void AddNewObject(IDrawContainer container, ObjectDraw o) {
      if (o == null) return;
      container.SelectAll(false);
      o.Selected = true;
      AddObjectDrawToGraphicsList(container, o);
      container.Capture = true;
      container.Refresh();
      
    }
    /// <summary>
    /// add object to last position
    /// </summary>
    /// <param name="container"></param>
    /// <param name="o"></param>
    protected virtual void AddObjectDrawToGraphicsList(IDrawContainer container, ObjectDraw o) {
      container.AddObjectDraw(o);
    }


    protected override void OnMouseMove(IDrawContainer container, ScrollMouseEventArgs e) {

      if (Current != null) {
        container.Cursor = Cursor;
        if (e.Button == MouseButtons.Left) {
          var handle = PointHandlerOnMouseMove(e);
          if (handle > 0) {
            Current.MovePointHandleTo(handle, new Point(e.X, e.Y));
            container.Refresh();
          }
        }
      }

    }

    protected override void OnMouseUp(IDrawContainer container, ScrollMouseEventArgs e) {

    }

    protected override void OnMouseClick(IDrawContainer container, ScrollMouseEventArgs e) {

    }

    protected override void OnMouseDoubleClick(IDrawContainer container, ScrollMouseEventArgs e) {

    }


    //public event EventHandler<ObjectDrawAddedEventArgs> ObjectDrawAdded;

    //protected void InvokeObjectDrawAdded(ObjectDrawAddedEventArgs e) {
    //  EventHandler<ObjectDrawAddedEventArgs> handler = ObjectDrawAdded;
    //  if (handler != null) handler(this, e);
    //}
  }
}
